var chargingStationNameList = [];
var bootstrapTableOption = {
    url: 'getEquipment',
    queryParams: function (params) {
        return {
            chargingStationID : $("#chargingStationID").val(),
            equipmentType : $("#equipmentType").val() || -1,      /* 设备类型（0：交流/1：直流）*/
            pageNumber: params.pageNumber,
            pageSize: params.pageSize
        }
    },
    columns: [
        {title: '序号', formatter: 'increasesWithZero'},
        {field: 'chargingStationName', title: '所属充电站'},
        {field: 'equipmentID', title: '桩编号'},
        {
            field: 'equipmentPower',
            title: '设备功率',
            formatter: function (value, row, index) {
                return value + "kW";
            }
        },
        {
            field: 'equipmentType',
            title: '交流/直流',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0 :
                        return ["<img class='icon-AC' src='static/images/icons/icon_AC.png'/>交流"].join('');
                    case 1 :
                        return ["<img class='icon-DC' src='static/images/icons/icon_DC.png'/>直流"].join('');
                }
            }
        },
        {field: 'businessID', title: '运营商'},
        {field: 'manufacturer', title: '生产厂家'},
        {field: 'manufacturerDTTM', title: '生产日期', formatter:"timestamp2date"},
        {
            title: '操作',
            formatter: function (value, row, index) {
                return ["<a class='table-operate' onclick='operateRefresh()'>刷新</a>"].join('');
            }
        }
    ],
    responseHandler: function (data) {
        return data;
    }
};

/*$(document).keydown(function (event) {
    if (event.keyCode == 13 && $("#chargingStationName").is(":focus")) {
        $('#queryButton').triggerHandler('click');
    }
});*/

$(document).ready(function () {

    setBootstrapTable('#equipmentTable', bootstrapTableOption); // 初始化表格
    getChargingStationName("#chargingStationID");
    setStatisticsHeader();  // 修改统计信息内容
});

function setStatisticsHeader() {
    $.ajax({
        url: "equipmentStatusStatistics",
        type: "post",
        dateType: "json",
        success: function (data) {
            for(var i = 0; i < data.length; i++)
                $("#type" + data[i].type).html(data[i].num);
        }
    });
}

function getChargingStationName(id) {
    changeSelectpicker(id, 'destroy');
    $(id).empty();
    if (chargingStationNameList && chargingStationNameList.length > 0) {
        setChargingStationName(id, chargingStationNameList);
    } else {
        $ajax("getChargingStationName", setChargingStationName, '', id, "get");
    }
};

function setChargingStationName(id, data) {
    var length = data.length;
    chargingStationNameList = data;
    for (var i = 0; i < length; i++) {
        $(id).append("<option value ='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
    }
    changeSelectpicker(id, 'refresh');
    $(id).selectpicker('setStyle', 'select', 'add');
    if (length > 5) {
        setSelectpicker(id);
    }
};

/* 表格操作：刷新 */
function operateRefresh() {
    
}

/* 根据查询条件获取充电站信息 */
function search() {

    $('#equipmentTable').bootstrapTable('selectPage', 1);
}

/* 重置 */
function reset() {

    /* 重置查询条件 */
    document.getElementById("chargingStationID").options.selectedIndex = 0;
    document.getElementById("equipmentType").options.selectedIndex = 0;
    $("#chargingStationID").selectpicker('refresh');
    $("#equipmentType").selectpicker('refresh');

    search();
}